package com.future.lock.home.activity;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.MessageEvent;
import com.future.baselib.utils.ConnectionUtils;
import com.future.baselib.utils.WifiAdmin;
import com.future.lock.R;
import com.future.lock.home.receiver.NetworkConnectChangeReceiver;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddGatewayStep2Activity extends BaseActivity implements TextWatcher {

    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.layout_title)
    FrameLayout layoutTitle;
    @BindView(R.id.tv_wifi_name)
    TextView tvWifiName;
    @BindView(R.id.et_wifi_password)
    EditText etWifiPassword;
    @BindView(R.id.iv_clear)
    ImageView ivClear;
    @BindView(R.id.iv_show_pwd)
    ImageView ivShowPwd;
    @BindView(R.id.tv_next)
    TextView tvNext;

    boolean showPwd = false;
    private NetworkConnectChangeReceiver receiver;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_add_gateway_step2);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        setTitle("添加网关");
        if (ConnectionUtils.isWifiConnected(this)) {
            tvWifiName.setText(WifiAdmin.getInstance(this).getWifiInfo().getSSID());
            tvNext.setEnabled(true);
        }else{
            tvWifiName.setText("手机未连接WiFi");
            tvNext.setEnabled(false);
        }
        etWifiPassword.addTextChangedListener(this);
    }

    @Override
    protected void initLogic() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        receiver = new NetworkConnectChangeReceiver();
        registerReceiver(receiver, filter);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @OnClick({R.id.iv_clear, R.id.iv_show_pwd, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_clear:
                etWifiPassword.setText("");
                break;
            case R.id.iv_show_pwd:
                int passwordLength = etWifiPassword.getText().length();
                etWifiPassword.setInputType(showPwd ?
                                (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD) :
                                (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD));
                ivShowPwd.setImageResource(showPwd ? R.mipmap.ic_hide_pwd : R.mipmap.ic_show_pwd);
                showPwd = !showPwd;
                etWifiPassword.setSelection(passwordLength);
                break;
            case R.id.tv_next:
                start();
                break;
        }
    }

    private void start() {
        String wifiSSID = tvWifiName.getText().toString();
        String password = etWifiPassword.getText().toString();
        if (TextUtils.isEmpty(password)) {
            toast("wifi密码不能为空");
            return;
        }
        if (password.length()<8) {
            toast("请正确输入wifi密码");
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString("ssid",wifiSSID);
        bundle.putString("pwd",password);
        startActivity(AddGatewayStep3Activity.class, bundle);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.toString().length()>0) {
            ivClear.setVisibility(View.VISIBLE);
        }else{
            ivClear.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void receiveEvent(MessageEvent event) {
        if (event.getCode() == NetworkConnectChangeReceiver.WIFI_CONNECT) {
            WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
            tvWifiName.setText(wifiManager.getConnectionInfo().getSSID());
            tvNext.setEnabled(true);
        }else if(event.getCode() == NetworkConnectChangeReceiver.WIFI_DISCONNECT){
            tvWifiName.setText("手机未连接WiFi");
            tvNext.setEnabled(false);
        }
    }
}
